﻿// jsoncpp_test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// Create by gaoyuan @20200729 for ZXJT

#include <iostream>
#include "json.h"
#include <fstream>
#include <stdio.h>
#include <ios>
/* 请求包示例：
{
	"F_FUNCTION":"L2930002",
	"STKBD":"01",
	"OPT_NUM":"9000001",
	"OPT_UNDL_CODE":"600001",
	"IS_REMIND":"1",
	"QUERY_FLAG":"1",
	"QRY_POS":"0",
	"QRY_NUM":"10"
}
*/
/* 应答包示例：
{
	"F_FUNCTION":"L2930002",
	"MSG_CODE":"0",
	"MSG_LEVEL":"0",
	"MSG_TEXT":"Success",
	"MSG_DEBUG":"xxx",
	"Result":[
	{
		"QRY_POS":"11",
		"STKEX":"01",
		"STKBD":"1",
		"OPT_NUM":"11",
		"OPT_CODE":"11",
		"OPT_NAME":"111",
		"OPT_TYPE":"11",
		"OPT_UNDL_CODE":"11",
		"OPT_UNDL_NAME":"11"
	},
	{
		"QRY_POS":"22",
		"STKEX":"02",
		"STKBD":"2",
		"OPT_NUM":"22",
		"OPT_CODE":"22",
		"OPT_NAME":"222",
		"OPT_TYPE":"22",
		"OPT_UNDL_CODE":"22",
		"OPT_UNDL_NAME":"22"
	}
}
*/


int main()
{
    //eg:一、构造请求包
	std::cout << "=====================构造请求报文开始==========================" << std::endl;
	Json::Value root;
	root["F_FUNCTION"] = "L2930002";//交易码标识
	root["OPT_NUM"] = "9000001";//合约
	root["OPT_UNDL_CODE"] = "600001";//标的
	root["IS_REMIND"] = "1";
	root["QRY_POS"] = "0";
	root["QRY_POS"] = "0";//定位串
	root["QRY_NUM"] = "10";//记录数
	std::cout << "输出请求包示例:" << std::endl;
	std::cout << root.toStyledString() << std::endl;			//格式化输出请求包
	std::cout << "=====================构造请求报文结束==========================" << std::endl;
	std::cout << "=====================解析应答报文开始==========================" << std::endl;
	//eg:二、解析应答包，应答包从文件中读取
	std::ifstream readfile("ans.json", std::ios::in);
	if (!readfile)
	{
		std::cout << "json file open error!" << std::endl;
		return 0;
	}
		
	Json::Value ansJson;
	Json::Reader jsonReader;
	if (jsonReader.parse(readfile, ansJson))
	{
		//第一步:解析第一结果集
		std::string msg_type = ansJson["F_FUNCTION"].asString();//解析消息编码
		std::string msg_code = ansJson["MSG_CODE"].asString();//解析第一结果集返回码
		std::string msg_level = ansJson["MSG_LEVEL"].asString();//解析第一结果集消息level
		std::string msg_text = ansJson["MSG_TEXT"].asString();//解析第一结果集返回信息
		std::string msg_debug = ansJson["MSG_DEBUG"].asString();//解析第一结果集消息debug
		//第二步:解析第二结果集
		int count = ansJson["Result"].size();//获取第二结果集记录数
		for (int i = 0; i < count; i++) //遍历第二结果集的每一条记录
		{
			std::string value1 = ansJson["Result"][i]["QRY_POS"].asString();
			std::string value2 = ansJson["Result"][i]["STKEX"].asString();
			std::string value3 = ansJson["Result"][i]["STKBD"].asString();
			std::string value4 = ansJson["Result"][i]["OPT_NUM"].asString();
			std::string value5 = ansJson["Result"][i]["OPT_CODE"].asString();
			std::string value6 = ansJson["Result"][i]["OPT_NAME"].asString();
			std::string value7 = ansJson["Result"][i]["OPT_TYPE"].asString();
			std::string value8 = ansJson["Result"][i]["OPT_UNDL_CODE"].asString();
			std::string value9 = ansJson["Result"][i]["OPT_UNDL_NAME"].asString();
			std::cout << "第" << i + 1 << "条记录：" << std::endl;
			std::cout << "value1=" << value1 << std::endl;
			std::cout << "value2=" << value2 << std::endl;
			std::cout << "value3=" << value3 << std::endl;
			std::cout << "value4=" << value4 << std::endl;
			std::cout << "value5=" << value5 << std::endl;
			std::cout << "value6=" << value6 << std::endl;
			std::cout << "value7=" << value7 << std::endl;
			std::cout << "value8=" << value8 << std::endl;
			std::cout << "value9=" << value9 << std::endl;
		}
		std::cout << "=====================解析应答报文结束==========================" << std::endl;
	}
	else
	{
		std::cout << jsonReader.getFormatedErrorMessages() << std::endl;
	}
	
}

